﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using GDFlacTool.Common;

namespace GDFlacTool.Forms
{
    public partial class FormArtistGenreEdit : Form
    {
        private readonly ArtistGenre _artistGenre;
        private readonly List<ArtistGenre> _list; 
        private readonly string _artist;
        private readonly string _genre;
        private readonly int _number;

        public FormArtistGenreEdit(ArtistGenre artistGenre, List<ArtistGenre> list)
        {
            InitializeComponent();

            _artistGenre = artistGenre;

            _artist = _artistGenre.Artist;

            _genre = _artistGenre.Genre;

            _number = _artistGenre.Id;

            _list = list;

            LoadGenreCombo();
        }

        private void FormArtistGenreEdit_Load(object sender, EventArgs e)
        {
            txtArtist.Text = _artist;

            if (cboGenre.Items.IndexOf(_genre) >= 0)
            {
                cboGenre.SelectedIndex = cboGenre.Items.IndexOf(_genre);
            }
            else
            {
                int genreIndex;

                if (int.TryParse(_genre, out genreIndex))
                {
                    cboGenre.SelectedIndex = cboGenre.Items.IndexOf(Genres.IndexToAudio(byte.Parse(genreIndex.ToString(CultureInfo.InvariantCulture))));
                }
                else
                {
                    cboGenre.Text = string.Empty;
                }
            }

            txtId.Text = string.Format("{0:0000#}", _number);
        }

        private void LoadGenreCombo()
        {
            string[] genres = Genres.Audio;

            for (int i = 0; i < Genres.Audio.Length; i++)
            {
                cboGenre.Items.Add(genres[i]);
            }
        }

        private bool CheckIfExists(string artist, int id)
        {
            ArtistGenre artistGenre = _list.Find(p => p.Artist == artist);

            if (artistGenre == null)
                return false;
            if (artistGenre.Artist == artist && artistGenre.Id != id)
                return true;
            return false;
        }

        private void txtArtist_TextChanged(object sender, EventArgs e)
        {
            if (txtArtist.Text != _artist || cboGenre.Text != _genre)
            {
                if (!string.IsNullOrEmpty(txtArtist.Text) && !string.IsNullOrWhiteSpace(txtArtist.Text))
                {
                    if (CheckIfExists(txtArtist.Text, _artistGenre.Id))
                    {
                        btnOK.Enabled = false;
                        txtArtist.ForeColor = Color.Red;
                    }
                    else
                    {
                        btnOK.Enabled = true;
                        txtArtist.ForeColor = Color.Black;
                    }
                }
                else
                {
                    btnOK.Enabled = false;
                }
            }
            else
            {
                btnOK.Enabled = false;
            }   
        }

        private void cboGenre_TextChanged(object sender, EventArgs e)
        {
            if (txtArtist.Text != _artist || cboGenre.Text != _genre)
            {
                if (!string.IsNullOrEmpty(cboGenre.Text) && !string.IsNullOrWhiteSpace(cboGenre.Text))
                {
                    if (CheckIfExists(txtArtist.Text, _artistGenre.Id))
                    {
                        btnOK.Enabled = false;
                        txtArtist.ForeColor = Color.Red;
                    }
                    else
                    {
                        btnOK.Enabled = true;
                        txtArtist.ForeColor = Color.Black;
                    }
                }
                else
                {
                    btnOK.Enabled = false;
                }
            }
            else
            {
                btnOK.Enabled = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _artistGenre.Artist = txtArtist.Text;
            _artistGenre.Genre = cboGenre.Text;
            _artistGenre.Status = ArtistGenreStatus.Edit;
        }
    }
}
